<?php
ini_set('date.timezone', 'Asia/Shanghai');
$path = sprintf("%s%swish-list.json", __DIR__, DIRECTORY_SEPARATOR);
$list = json_decode(file_get_contents($path), true);

$msg = '';
$op  = $_POST['op'] ?? '';
if ($op === '') {
    $op = $_GET['op'] ?? '';
}
if ($op === 'add') {
    $p = $_POST['price'] ?? false;
    $u = $_POST['url'] ?? false;
    $t = $_POST['title'] ?? false;
    if ($p && $u && $t) {
        $list[] = [
            0,  // 当前价格 港币
            intval($p), // 期望价格 人民币
            $u, // url
            $t, // 名字
            0, // 更新时间
            time() // 添加时间
        ];
        file_put_contents($path, json_encode($list, JSON_UNESCAPED_UNICODE));
    } else {
        $msg = "参数不能为空";
    }
} elseif ($op === 'del') {
    foreach ($list as $idx => $item) {
        if ($item[2] === $_GET['url']) {
            unset($list[$idx]);
            break;
        }
    }
    $list = array_values($list);
    file_put_contents($path, json_encode($list, JSON_UNESCAPED_UNICODE));
}
?>

<html lang="zh">
<head>
    <title>PS Store价格监控</title>
    <style>
        .input {
            height: 32px;
            outline: none;
        }

        a:link {
            color: #3C3C3C;
            text-decoration: underline;
        }

        a:visited {
            color: #0000FF;
            text-decoration: none;
        }

        a:hover {
            color: #FF00FF;
            text-decoration: none;
        }

        a:active {
            color: #D200D2;
            text-decoration: none;
        }
    </style>
</head>
<body>
<div style="width: 800px;margin: 0 auto;">
<p style="text-align: left"><a href="index.php">回到主页</a> 消息:<?php echo $msg; ?></p>
<div style="width: 800px;border: 1px solid #ccc;padding: 5px 15px;">
    <p style="text-align: left;">列表:</p>
    <table style="text-align:center;width: 100%">
        <tr>
            <th>序号</th>
            <th>游戏名称</th>
            <th>当前价格</th>
            <th>期望价格</th>
            <th>最近更新</th>
            <th>添加时间</th>
            <th>操作</th>
        </tr>

        <?php
        foreach ($list as $idx => $item) {
            $i = $idx + 1;
            echo "<tr><td>{$i}</td>"
                . "<td><a href='{$item[2]}' target='_blank'>{$item[3]}</a></td>"
                . "<td>HK$ {$item[0]}</td>"
                . "<td>¥ {$item[1]}</td>"
                . "<td>".date('Y-m-d H:i:s', $item[4])."</td>"
                . "<td>".date('Y-m-d', $item[5])."</td>"
                . "<td><a href='index.php?op=del&url={$item[2]}'>删除</a></td></tr>";
        }
        ?>

    </table>
</div>
<p></p>
<div style="width: 500px;border: 1px solid #ccc;padding: 5px 15px;text-align: left;">
    <form action="index.php" method="post">
        <input type="hidden" name="op" value="add">
        <p>游戏名称: <input type="text" name="title" class="input"/></p>
        <p>期望价格: <input type="text" name="price" class="input"/> ¥</p>
        <p>游戏网址: <input type="text" name="url" class="input" style="width: 400px"/></p>
        <input type="submit" value="添加" class="input" style="width: 100px;cursor: pointer"/>
    </form>
</div>
</div>
</body>
</html>